package com.xf.tourismapp.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.xf.tourismapp.R;
import com.xf.tourismapp.fragments.FoundFragment;
import com.xf.tourismapp.fragments.HomePageFragment;
import com.xf.tourismapp.fragments.CheckWorkFragment;
import com.xf.tourismapp.fragments.MyFragment;
import com.xf.tourismapp.util.HttpCallbackListener;
import com.xf.tourismapp.util.HttpUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class MainActivity extends FragmentActivity implements AdapterView.OnItemClickListener {
    //private FrameLayout fg_content;
    private TextView tv_content;
    private RadioButton rb_homePage;
    private RadioButton checkwork;
    private RadioButton rb_found;
    private RadioButton rb_my;
    private FrameLayout fg_content;
    //定义Fragment对象
    private MyFragment mMyFragment;
    private HomePageFragment mHomePageFragment;
    private CheckWorkFragment checkWorkFragment;
    private FoundFragment mFoundFragment;
    private FragmentTransaction ft;
    private String account;//账号信息
//    LinearLayout tbtitle;


    private String originAddress = "http://2qmaev.natappfree.cc/student_manager/user/login";

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        //获取从LoginActivity传递过来的账号信息
        account = getIntent().getStringExtra("user") + getString(R.string.main_page_location_info_prefix);
        setSelection(3);
    }

    private void initView() {
        // fg_content = (FrameLayout) findViewById(R.id.fg_content);
//        tbtitle = (LinearLayout) findViewById(R.id.tb_title);
        fg_content = (FrameLayout) findViewById(R.id.fg_content);
        tv_content = (TextView) findViewById(R.id.tv_content);
        rb_homePage = (RadioButton) findViewById(R.id.rb_homePage);
        checkwork = (RadioButton) findViewById(R.id.checkwork);
        rb_found = (RadioButton) findViewById(R.id.rb_found);
        rb_my = (RadioButton) findViewById(R.id.rb_my);
    }

    /**
     * 点击底部导航RadioButton菜单栏后，改变RadioButton图标和文字变成蓝色,
     * 同时加载对应的Fragment页面内容
     *
     * @param index RadioButton在RadioGroup中的序号
     */
    private void setSelection(int index) {
        resetImg();
        //创建FragmentTransaction
        ft = getSupportFragmentManager().beginTransaction();
        //隐藏所有Fragment
        hideFragments(ft);
        //根据选择底部导航栏按钮切换加载对应内容的Fragment页面
        switch (index) {
            case 0:
                showContent(0);
                homePageFragmentLoad();
                break;
            case 1:
                showContent(1);
                musicFragmentLoad();
                break;
            case 2:
                showContent(2);
                foundFragmentLoad();
                break;
            case 3:
                showContent(3);
                myFragmentLoad();
                break;
        }
    }

    //设置点击RadioButton文字和背景颜色并加载MyFragment
    private void myFragmentLoad() {
//        tbtitle.setVisibility(View.GONE);
        tv_content.setVisibility(View.VISIBLE);
        rb_my.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_my_on, 0, 0);
        rb_my.setTextColor(getColor(R.color.lightblue));
        if (mMyFragment == null) {
            mMyFragment = new MyFragment();
            ft.add(R.id.fg_content, mMyFragment);
        }
        ft.show(mMyFragment);
    }

    //设置点击RadioButton文字和背景颜色并加载FoundFragment
    private void foundFragmentLoad() {
//        tbtitle.setVisibility(View.GONE);
        tv_content.setVisibility(View.VISIBLE);
        rb_found.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_found_on, 0, 0);
        rb_found.setTextColor(getColor(R.color.lightblue));
        if (mFoundFragment == null) {
            mFoundFragment = new FoundFragment();
            ft.add(R.id.fg_content, mFoundFragment);
        }
        ft.show(mFoundFragment);
    }

    //设置点击RadioButton文字和背景颜色并加载MusicFragment
    private void musicFragmentLoad() {
//        tbtitle.setVisibility(View.GONE);
        tv_content.setVisibility(View.VISIBLE);
        checkwork.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_music_on, 0, 0);
        checkwork.setTextColor(getColor(R.color.lightblue));
        if (checkWorkFragment == null) {
            checkWorkFragment = new CheckWorkFragment();
            ft.add(R.id.fg_content, checkWorkFragment);
        }
        ft.show(checkWorkFragment);
    }

    //设置点击RadioButton文字和背景颜色并加载HomePageFragment
    private void homePageFragmentLoad() {
//        tbtitle.setVisibility(View.VISIBLE);
        tv_content.setVisibility(View.GONE);
        //变换图标
        rb_homePage.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_homepage_on, 0, 0);
        //变换文字颜色
        rb_homePage.setTextColor(getColor(R.color.lightblue));
        if (mHomePageFragment == null) {
            mHomePageFragment =    HomePageFragment.getHomePageFragmentInstance();
//            mHomePageFragment = HomePageFragment.getHomePageFragmentInstance();
//            mHomePageFragment.init();
            ft.add(R.id.fg_content, mHomePageFragment);
        }
        ft.show(mHomePageFragment);
//        try {
//            //构造完整URL
//            String compeletedURL = HttpUtil.getURLWithParams(originAddress, new HashMap<>());
            //发送请求
//            HttpUtil.sendHttpRequest(compeletedURL, new HttpCallbackListener() {
//                @Override
//                public void onFinish(String response) {
//                    Message message = new Message();
//                    message.obj = response;
//                    mHandler.sendMessage(message);
//                }
//
//                @Override
//                public void onError(Exception e) {
//                    Message message = new Message();
//                    message.obj = e.toString();
//                    mHandler.sendMessage(message);
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //设置默认被选中RadioButton导航栏为个人中心“我的”
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(this, "你选择的项目是：" + ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideFragments(FragmentTransaction ft) {
        if (mHomePageFragment != null) {
            ft.hide(mHomePageFragment);
        }
        if (checkWorkFragment != null) {
            ft.hide(checkWorkFragment);
        }
        if (mFoundFragment != null) {
            ft.hide(mFoundFragment);
        }
        if (mMyFragment != null) {
            ft.hide(mMyFragment);
        }
        ft.commit();
    }

    /**
     * 恢复所有RadioButton默认颜色和图片
     */
    private void resetImg() {
        rb_homePage.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_homepage, 0, 0);
        rb_homePage.setTextColor(getColor(R.color.tab_text_bg));

        rb_found.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_found, 0, 0);
        rb_found.setTextColor(getColor(R.color.tab_text_bg));

        rb_my.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_my, 0, 0);
        rb_my.setTextColor(getColor(R.color.tab_text_bg));
    }

    /**
     * 显示页面内容和账号信息
     */
    private void showContent(int index) {
        switch (index) {
            case 0: {
                tv_content.setText(account.concat(getString(R.string.main_page_location_info_1)));
                break;
            }
            case 1: {
                tv_content.setText(account.concat("考勤页面"));
                break;
            }
            case 2: {
                tv_content.setText(account.concat(getString(R.string.main_page_location_info_3)));
                break;
            }
            case 3: {
                tv_content.setText(account.concat(getString(R.string.main_page_location_info_4)));
                break;
            }
        }
    }


    //选择主页页面
    public void onHomePageClicked(View view) {
        setSelection(0);
    }

    //选择考勤页面
    public void onOrderClicked(View view) {
        setSelection(1);
    }

    //选择发现页面
    public void onFoundClicked(View view) {
        setSelection(2);
    }

    //选择我的页面
    public void onMyClicked(View view) {
        setSelection(3);
    }
}